/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.txt.access.capability.libraries.rabbitmq.impl.la_publisher;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.MessageProperties;
import it.txt.access.capability.libraries.rabbitmq.AbstractApplication;
import it.txt.access.capability.libraries.rabbitmq.exception.AbstractApplicationException;
import it.txt.access.capability.libraries.rabbitmq.settings.AbstractApplicationSettings;
import static it.txt.access.capability.libraries.rabbitmq.impl.publisher.PublisherApplication.*;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;

/**
 *
 * @author seccia
 */
public class LAPublisherApplication extends AbstractApplication {

    public static final String LM_PUBLISHER_SPECIAL_DIR = "%S";
    private AMQP.BasicProperties amqpProps;
    private String realLogAndAuditNodeID;
    private String lastLogType;

    public LAPublisherApplication(AbstractApplicationSettings settings) throws AbstractApplicationException {
        super(settings);
    }
    
    @Override
    public void sessionRenegotiation() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void logAndAuditPublish(String messageBody, String logType) throws AbstractApplicationException {
        LOGGER.log(Level.INFO, "Publishing message: {0}", messageBody);
        try {
            getCapabilityConnection().getChannel().basicPublish(
                    // Eschenage ad the element returned by the response
                    getCapabilityConnection().getQueueName(),
                    // Recover Routing Key by the log type
                    getLogAndAuditNodeID(logType),
                    //recover the properties
                    getBasicProperties(),
                    //recover the message bytes
                    messageBody.getBytes());
            //Ok message published
            LOGGER.log(Level.INFO, "Message published.");
        }
        catch (Exception ex) {
            ex.printStackTrace();
            throw new AbstractApplicationException(ex.getMessage(), ex);
        }
    }

    private AMQP.BasicProperties getBasicProperties(){
        if(amqpProps == null){
            amqpProps = MessageProperties.BASIC;
            Map<String, Object> headers = new HashMap<String, Object>();
            headers.put(CHANNEL_PUB_PROPS_ID,
                    ((LAPublisherAppModel)applicationModel).getSignerInfo().getSignerID());
            headers.put(CHANNEL_PUB_PROPS_TIME_STAMP_LONG,
                    new Date().getTime());
            headers.put(CHANNEL_PUB_PROPS_ENTITY_DATA_URI, 
                    ((LAPublisherAppModel)applicationModel).getEntityDataURI());
            headers.put(CHANNEL_PUB_PROPS_ENTITY_URI, 
                    ((LAPublisherAppModel)applicationModel).getEntityURI());
        }
        return amqpProps;
    }

    private String getLogAndAuditNodeID(String logType){
        if(!logType.equals(lastLogType)){
            lastLogType = logType;
            String logAndAuditNodeID = ((LAPublisherAppModel)applicationModel).getLogAndAuditNodeID();
            String pattern = logAndAuditNodeID.split(PUBLISHER_PATTERN_SEPARATOR)[1];
            realLogAndAuditNodeID = pattern.replace(LM_PUBLISHER_SPECIAL_DIR, logType);
        }
        LOGGER.log(Level.INFO, "Publisher Leaf Node ID: {0}", realLogAndAuditNodeID);

        return realLogAndAuditNodeID;
    }
}
